<template>
    <el-dialog
            :title="!dataForm.orderId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="销售记录唯一标识" prop="saleId">
                        <el-input v-model.number="dataForm.saleId" placeholder="销售记录唯一标识"></el-input>
                </el-form-item>
                <el-form-item label="关联客户ID" prop="customerId">
                        <el-input v-model.number="dataForm.customerId" placeholder="关联客户ID"></el-input>
                </el-form-item>
                <el-form-item label="关联产品ID" prop="productId">
                        <el-input v-model.number="dataForm.productId" placeholder="关联产品ID"></el-input>
                </el-form-item>
                <el-form-item label="关联销售人员ID" prop="salespersonId">
                        <el-input v-model.number="dataForm.salespersonId" placeholder="关联销售人员ID"></el-input>
                </el-form-item>
                <el-form-item label="销售日期" prop="saleDate">
                        <el-date-picker
                                v-model="dataForm.saleDate"
                                type="datetime"
                                placeholder="销售日期">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="销售金额" prop="saleAmount">
                        <el-input v-model="dataForm.saleAmount" placeholder="销售金额"></el-input>
                </el-form-item>
                <el-form-item label="销售数量" prop="quantity">
                        <el-input v-model.number="dataForm.quantity" placeholder="销售数量"></el-input>
                </el-form-item>
                <el-form-item label="支付方式" prop="paymentMethod">
                        <el-input v-model="dataForm.paymentMethod" placeholder="支付方式"></el-input>
                </el-form-item>
                <el-form-item label="销售状态（如已完成、已取消等）" prop="status">
                        <el-input v-model="dataForm.status" placeholder="销售状态（如已完成、已取消等）"></el-input>
                </el-form-item>
                <el-form-item label="记录创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="记录创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="记录更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="记录更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
        data () {
            return {
                visible: false,
                dataForm: {
                    saleId: null,
                    customerId: null,
                    productId: null,
                    salespersonId: null,
                    saleDate: null,
                    saleAmount: null,
                    quantity: null,
                    paymentMethod: null,
                    status: null,
                    createdAt: null,
                    updatedAt: null,
                },
                dataRule: {
                    saleId: [
                        { required: true, message: '销售记录唯一标识不能为空', trigger: 'blur' }
                    ],
                    customerId: [
                        { required: true, message: '关联客户ID不能为空', trigger: 'blur' }
                    ],
                    productId: [
                        { required: true, message: '关联产品ID不能为空', trigger: 'blur' }
                    ],
                    salespersonId: [
                        { required: true, message: '关联销售人员ID不能为空', trigger: 'blur' }
                    ],
                    saleDate: [
                        { required: true, message: '销售日期不能为空', trigger: 'blur' }
                    ],
                    saleAmount: [
                        { required: true, message: '销售金额不能为空', trigger: 'blur' }
                    ],
                    quantity: [
                        { required: true, message: '销售数量不能为空', trigger: 'blur' }
                    ],
                    paymentMethod: [
                        { required: true, message: '支付方式不能为空', trigger: 'blur' }
                    ],
                    status: [
                        { required: true, message: '销售状态（如已完成、已取消等）不能为空', trigger: 'blur' }
                    ],
                    createdAt: [
                        { required: true, message: '记录创建时间不能为空', trigger: 'blur' }
                    ],
                    updatedAt: [
                        { required: true, message: '记录更新时间不能为空', trigger: 'blur' }
                    ],
                }
            }
        },
        methods: {
            init (id) {
                this.dataForm.orderId = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    this.dataForm.orderId = id || 0
                    if (this.dataForm.orderId) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppSales/info/\\$\\{this.dataForm.orderId}`),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.dataForm = data.appsales
                            }
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit () {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.$http({
                            url: this.$http.adornUrl(`/app/AppSales/\\$\\{!this.dataForm.orderId ? 'save' : 'update'}`),
                            method: 'post',
                            data: this.$http.adornData(this.dataForm)
                        }).then(({data}) => {
                            if (data && data.code === 0) {
                                this.$message({
                                    message: '操作成功',
                                    type: 'success',
                                    duration: 1500,
                                    onClose: () => {
                                        this.visible = false
                                        this.$emit('refreshDataList')
                                    }
                                })
                            } else {
                                this.$message.error(data.msg)
                            }
                        })
                    }
                })
            }
        }
    }
</script>